﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.Data.Entity;

namespace GPS.Controllers.GPS_DanhMuc
{
    public class GPS_LoaiHoSoController : Controller
    {
        //
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        //
        // GET: /GPS_HTCapGPLX/
        #region Index
        public ActionResult Index(ViewModelSHOSO SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            var entities = _db.Filter<SHOSO>(o => (SearchModel.hHOSO_MA == null || o.HOSO_MA.StartsWith(SearchModel.hHOSO_MA))
                                        && (SearchModel.hHOSO_TEN == null || o.HOSO_TEN.StartsWith(SearchModel.hHOSO_TEN))
                                        && (SearchModel.hHOSO_VBPL == null || o.HOSO_VBPL.StartsWith(SearchModel.hHOSO_VBPL))
                                        && (SearchModel.hHOSO_ACTIVE == null || o.HOSO_ACTIVE == SearchModel.hHOSO_ACTIVE));
            ViewBag.TotalRow = entities.Count();
            var model = new List<SHOSO>();
            foreach (var currentEntity in entities)
            {
                model.Add(Mapper.Map<SHOSO>(currentEntity));
            }
            Int16 icount = 1;
            foreach (var item in model)
            {
                item.STT = icount;
                icount++;
            }
            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View(SearchModel);
        }

        [HttpPost]
        public void initialCategorySearchAction(ViewModelSHOSO viewModel)
        {
            //ViewBag.lstActivedll = new SelectList(_db.GetAll<STRANGTHAI>(), "TRANGTHAI_MA", "TRANGTHAI_TEN");
            //viewModel.listTrangThai = _db.GetAll<STRANGTHAI>();
        }
        #endregion

        //
        // Create: 
        #region create
        public ActionResult Create()
        {
            initialCategoryCreateAction();
            return View(new CrudModelSHOSO());
        }

        [HttpPost]
        public ActionResult Create(CrudModelSHOSO model, FormCollection form)
        {
            initialCategoryCreateAction();
            if (ModelState.IsValid)
            {
                SHOSO _model = new SHOSO();
                var str = Convert.ToInt64(_db.GetAll<SHOSO>().Max(x => x.HOSO_MA).ToString());
                GPS.COMMON.Helpers.CopyObject<SHOSO>(model, ref _model);
                _model.HOSO_MA = GetValue_Indentity();
                _db.Insert<SHOSO>(_model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
        public void initialCategoryCreateAction()
        {
            ViewBag.HOSO_HTGPLX_MAss = new SelectList(_db.GetAll<SHTCAPGPLX>(), "HTGPLX_MA", "HTGPLX_TEN");
            ViewBag.HOSO_NDSH_MAss = new SelectList(_db.GetAll<SNOIDUNGSH>(), "NDSH_MA", "NDSH_TEN");
            ViewBag.HOSO_NHOMSS = new SelectList(_db.Filter<SHOSO>(x => x.HOSO_NHOM == "0"), "HOSO_MA", "HOSO_TEN");
        }

        public string GetValue_Indentity()
        {
            string value = "";
            Int64 valueMax = _db.DbContext.SHOSOes.ToList().Max(e => Convert.ToInt64(e.HOSO_MA));
            value = (valueMax + 1).ToString();
            return value;
        }
        #endregion

        //
        // Edit: 
        #region Edit
        public ActionResult Edit(string id)
        {
            CrudModelSHOSO _model = new CrudModelSHOSO();
            SHOSO model = _db.GetOne<SHOSO>(o => o.HOSO_MA == id);
            GPS.COMMON.Helpers.CopyObject<CrudModelSHOSO>(model, ref _model);
            initialCategoryEditAction(_model);
            return View(_model);
        }
        [HttpPost]
        public ActionResult Edit(CrudModelSHOSO model, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                var _model = _db.GetOne<SHOSO>(o => o.HOSO_MA == model.HOSO_MA);
                GPS.COMMON.Helpers.CopyObject<SHOSO>(model, ref _model);
                _db.Update<SHOSO>(_model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
        public void initialCategoryEditAction(CrudModelSHOSO view)
        {
            ViewBag.HOSO_HTGPLX_MAss = new SelectList(_db.GetAll<SHTCAPGPLX>(), "HTGPLX_MA", "HTGPLX_TEN");
            ViewBag.HOSO_NDSH_MAss = new SelectList(_db.GetAll<SNOIDUNGSH>(), "NDSH_MA", "NDSH_TEN");
            string selectedValuesNhom = _db.GetAll<SHOSO>().Select(x => x.HOSO_NHOM = view.HOSO_NHOM).FirstOrDefault();
            ViewBag.HOSO_NHOMSS = new SelectList(_db.Filter<SHOSO>(x => x.HOSO_NHOM == "0"), "HOSO_MA", "HOSO_TEN", selectedValuesNhom);
        }
        #endregion

        #region Detele
        public ActionResult Delete(string id)
        {
            CrudModelSHOSO _model = new CrudModelSHOSO();
            SHOSO model = _db.GetOne<SHOSO>(o => o.HOSO_MA == id);
            _db.DeleteItem<SHOSO>(model);
            return RedirectToAction("Index");
        }
        #endregion

        #region Detail
        public ActionResult Detail(string id)
        {
            CrudModelSHOSO _model = new CrudModelSHOSO();
            SHOSO model = _db.GetOne<SHOSO>(o => o.HOSO_MA == id);
            GPS.COMMON.Helpers.CopyObject<CrudModelSHOSO>(model, ref _model);
            SHTCAPGPLX htcap = _db.GetOne<SHTCAPGPLX>(o => o.HTGPLX_MA == _model.HOSO_HTGPLX_MA);
            SNOIDUNGSH nd = _db.GetOne<SNOIDUNGSH>(o => o.NDSH_MA == _model.HOSO_NDSH_MA);

            if (htcap != null)
            {
                _model.HOSO_HTGPLX_MA = htcap.HTGPLX_TEN == null ? null : htcap.HTGPLX_TEN;
            }
            if (nd != null)
            {
                _model.HOSO_NDSH_MA = nd.NDSH_TEN == null ? null : nd.NDSH_TEN;
            }

            if (_model.HOSO_ACTIVE == "1")
            {
                _model.HOSO_ACTIVE = "Hiệu Lực";
            }
            else
            {
                _model.HOSO_ACTIVE = "Không Hiệu Lực";
            }
            return PartialView("Detail", _model);
        }
        #endregion
    }
}